package com.cn.ant.modules.user.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.modules.user.dao.UserMainDao;
import com.cn.ant.modules.user.entity.UserMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 用户信息Service
 * @author 黄根华
 * @version 2015-09-26
 */
@Service
@Transactional(readOnly = true)
public class UserMainService extends BaseService {

    @Autowired
	private UserMainDao userMainDao;

	public UserMain get(String id) {
		return userMainDao.get(id);
	}
	
	public List<UserMain> findList(UserMain userMain) {
		return userMainDao.findList(userMain);
	}
	
	public Page<UserMain> findPage(Page<UserMain> page, UserMain userMain) {
	    userMain.setPage(page);
        page.setList(userMainDao.findList(userMain));
        return page;
	}

	/**
	 * 根据openId查询用户信息
	 * @param appId
	 * @param openId
	 */
	public UserMain queryUserByOpenId(String appId, String openId) {
		return userMainDao.queryUserByOpenId(appId, openId);
	}
	
	@Transactional(readOnly = false)
	public void save(UserMain userMain) {
	    if (userMain.getIsNewRecord()) {
            userMain.preInsert();
            userMainDao.insert(userMain);
        } else {
            userMain.preUpdate();
            userMainDao.update(userMain);
        }
	}
	
	@Transactional(readOnly = false)
	public void delete(UserMain userMain) {
		userMainDao.delete(userMain);
	}
	
}